package CountDownLatch_5;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class 服务健康检查_多服务启动验证 {
    public static void main(String[] args) throws InterruptedException {
        final String[] SERVICES = {"数据库", "缓存", "消息队列", "文件存储", "外部API"};
        final CountDownLatch healthCheckLatch = new CountDownLatch(SERVICES.length);

        // 启动健康检查线程
        for (String service : SERVICES) {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        System.out.println("正在检查 " + service + " 服务健康状态...");
                        Thread.sleep((long)(Math.random() * 1000)); // 模拟检查时间

                        boolean healthy = Math.random() > 0.2; // 80%概率健康
                        if (healthy) {
                            System.out.println(service + " 服务健康检查通过");
                            healthCheckLatch.countDown();
                        } else {
                            System.out.println(service + " 服务健康检查失败");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        System.out.println("等待所有服务健康检查...");
        boolean allHealthy = healthCheckLatch.await(3, TimeUnit.SECONDS); // 最多等待3秒

        if (allHealthy) {
            System.out.println("所有服务健康检查通过，系统可以启动");
        } else {
            System.out.println("部分服务健康检查失败，系统不能启动");
        }
    }
}
